package TController
{
	import mx.events.IndexChangedEvent;
	
	public class TextUtils
	{
		public function TextUtils()
		{
		}
		
		public static function minimum(a:int,b:int,c:int):int
		{
			var mi:int=a;
			if(b < mi)mi= b
			if(c<mi)mi=c
			
			return mi;  
		}
		
		public static function LD(s:String,t:String):int
		{
			var d:Array=new Array(); //matrix
			var m:int; 
			var n:int;
			var i:int;
			var j:int;
			var s_i:String="";
			var t_j:String="";
			var cost:int;
			
			n=s.length;
			m=t.length;
			if(n==0)return m;
			if(m==0)return n;
			for(i=0;i<n;i++){
			 d[i]=new Array();
			 d[i][0]=i
			};
			for(j=0;j<m;j++)
			 d[0][j]=j
			for(i=1;i<n;i++){
			 s_i=s.charAt(i);
			 for(j=1;j<m;j++){
			 	t_j=t.charAt(j);
			 	if(s_i==t_j)cost=0
			 	 else cost=1;
			 	d[i][j]=minimum(d[i-1][j]+1,d[i][j-1]+1,d[i-1][j-1]+cost)			 	

			 };
			}
		return d[n-1][m-1];

			
		}
		
		static private function gestalt(padrao:String, texto: String, tpadrao:int,ttexto:int):int
		{
    		var p:String="";
    		var q:String="";;
			var max:int= 1;
			var end1:int=  ttexto + 1;   
			var end2:int=  tpadrao + 1;
			
			var l:int;   
			var pos1:int=1;
			var pos2:int=1;
			var sum:int=0;
			p=texto;var cont1:int=1;var cont2:int;
   			while(cont1 < end1){
         	q=padrao;
         	cont2=1;
             while(cont2 < end2){
               l=1;
                    while((((cont1 + l) - 1) < end1) && (((cont2 + l)-1) < end2)&& (p.charAt((cont1+l)-1) == q.charAt((cont2+l)-1)))l++;
               if(l > max){                    
                      max = l;
                      pos1 = (texto.length - (texto.length-(cont1-1)))+1;
                      pos2 = (padrao.length - (padrao.length-(cont2-1)))+1;
               }
               cont2++;
            }
         cont1++;
   			}
   if (max == 1)return 0
      else {
             sum = max;
             if((pos1>1)&&(pos2>1))
                 sum = sum + gestalt(padrao, texto, pos2-1, pos1-1);
             if ((((pos1 + max - 1) < tpadrao) &&((pos2 + max - 1) < ttexto)))
                
                  sum = sum + gestalt(padrao.substr(pos2 + max - 1,padrao.length), texto.substr(pos1 + max - 1,texto.length), tpadrao - pos2 - max , ttexto - pos1 - max);
                
             return sum-1;
             
      }

	}


		public static function gestalt2(s1:String,s2:String):int
		{
			return (100*(gestalt(s1,s2,s1.length,s2.length) * 2 / ( s1.length + s2.length ))); 
		} 

	}
}